﻿namespace WMS6.Web
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web.BaseInfoServiceRef;

    public class Department : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected HtmlForm F2641;
        protected HtmlInputHidden hdnFrom;
        protected Label lblDepartment;
        protected Label lblDepartmentDescr;
        protected Label lblDepartmentID;
        protected Label lblDPContact;
        protected Label lblDPContactPhone;
        protected Label lblMsg;
        protected Label lblRemark;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDepartmentDescr;
        protected TextBox txtDepartmentID;
        protected TextBox txtDPContact;
        protected TextBox txtDPContactPhone;
        protected TextBox txtRemark;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtDepartmentID.Text = "";
            this.txtDepartmentID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtDepartmentID.Text.Trim()))
            {
                try
                {
                    List<string> departmentID = new List<string> {
                        this.txtDepartmentID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(departmentID, "DEPARTMENT") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + "此部门有员工记录,请先删除此部门的员工的信息再删除此部门。";
                }
            }
            else
            {
                this.lblMsg.Text = "没有保存,不能删除";
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("DepartmentQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("DepartmentList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepartmentID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2640_001");
            }
            else if (string.IsNullOrEmpty(this.txtDepartmentDescr.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2640_002");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateDepartmentInfo();
            }
            else
            {
                this.InsertDepartmentInfo();
            }
        }

        private void InsertDepartmentInfo()
        {
            try
            {
                DepartmentInfo departmentInfo = new DepartmentInfo {
                    DepartmentID = this.txtDepartmentID.Text.Trim(),
                    Descr = this.txtDepartmentDescr.Text.Trim(),
                    DPContact = this.txtDPContact.Text.Trim(),
                    DPContactPhone = this.txtDPContactPhone.Text.Trim(),
                    Remark = this.txtRemark.Text.Trim(),
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(departmentInfo, "DEPARTMENT") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = departmentInfo;
                this.txtDepartmentID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetDepartmentInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetDepartmentInfo2Form()
        {
            try
            {
                string departmentID = base.Request.QueryString["ID"].ToString();
                DepartmentInfo departmentInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(departmentID, "DEPARTMENT") as DepartmentInfo;
                base.DataEntityViewState = departmentInfo;
                if (departmentInfo != null)
                {
                    this.txtDepartmentID.Text = departmentInfo.DepartmentID;
                    this.txtDepartmentDescr.Text = departmentInfo.Descr;
                    this.txtDPContact.Text = departmentInfo.DPContact;
                    this.txtDPContactPhone.Text = departmentInfo.DPContactPhone;
                    this.txtRemark.Text = departmentInfo.Remark;
                    this.chkIsActive.Checked = departmentInfo.IsActive == "Y";
                    this.txtDepartmentID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateDepartmentInfo()
        {
            try
            {
                DepartmentInfo departmentInfo = base.DataEntityViewState as DepartmentInfo;
                departmentInfo.Descr = this.txtDepartmentDescr.Text.Trim();
                departmentInfo.DPContact = this.txtDPContact.Text.Trim();
                departmentInfo.DPContactPhone = this.txtDPContactPhone.Text.Trim();
                departmentInfo.Remark = this.txtRemark.Text.Trim();
                departmentInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                departmentInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(departmentInfo, "DEPARTMENT") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

